<?php

require_once 'facebook/facebook.php';

class Social_Plugin_Provider_Facebook extends Social_Plugin_Provider_Abstract
{

	protected $_name = 'facebook';

	/**
	 * do init called whenever it's startup.
	 */
	public function init()
	{
		$this -> _options = Agis::getTable('core@setting') -> getAll('social.facebook');

		$this -> _plugin = new Facebook( array(
				'app_id' => $this -> getOption('key'),
				'secret' => $this -> getOption('secret'),
		));
	}

	public function getAuthUrl($params = array(), $callback = null)
	{
		$params['perms'] = 'email';
		$callback = Agis::getUrl('social/connect/facebook/callback');
		$cancelUrl = Agis::getUrl('social/connect/facebook/cancel');

		return $this -> _plugin -> getLoginUrl($callback, $cancelUrl, $params);
	}

	public function getLogoutUrl()
	{
		$next = Agis::getUrl('');
		return $this -> _plugin -> getLogoutUrl($next);
	}

	public function isConnected()
	{

	}

	public function isLogged()
	{

	}

	/**
	 * process do connect by session return data
	 * @param Request $request
	 * @return void
	 */
	public function doConnect($request = null)
	{
		$session = $request -> getParam('session');

		$session = json_decode($session, 1);

		$this -> _plugin -> setSession($session);

		$me = $this -> _plugin -> api('/me');

		// test that session is work fine.

		$_SESSION['facebook'] = $session;

		// call hook after process
		Agis::hasListener('afterFacebookConnect') && Agis::fireEvent('afterFacebookConnect');
	}

	/**
	 * process do logout
	 * @param Request $request
	 * @return void
	 */
	public function doLogout($request = null)
	{
		unset($_SESSION['facebook']);
		Agis::hasListener('afterFacebookDisconnect') && Agis::fireEvent('afterFacebookDisconnect');
	}

}
